<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\UserCreateRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use App\Userdetail;
use Hash;
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $keywords = $request->keywords;
        $user = User::orderBy('id','asc')->where(function($query)use($keywords){
            if(!empty($keywords)){
                $query->where('name','like','%'.$keywords.'%');
            }
        })->paginate($request->input('num',10));
        //显示用户列表页
        return view('webadmin.user.list',['user'=>$user,'data'=>$request->all()]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('webadmin.user.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(UserCreateRequest $request)
    {
        //表单验证
    //     $this->validate($request, [
    //     'name' => 'required|unique:users,name|regex:/^\w{6,20}$/',
    //     'password' => 'required|regex:/^\S{6,20}$/|same:repassword',
    //     'email'=>'required|email'
    // ],[
    //     'name.required'=>'用户名必填',
    //     'name.unique'=>'用户名已经存在',
    //     'name.regex'=>'用户名格式不正确',
    //     'password.required'=>'密码必填',
    //     'password.regex'=>'密码格式不正确',
    //     'password.same'=>'两次输入密码不同',
    //     'email.required'=>'邮箱必填',
    //     'email.email'=>'请填写正确的邮箱'
    // ]);

        //验证成功 数据库入库
        $user = new User;
        $user['name'] = $request->name;
        $user['password'] = Hash::make($request->password);
        $user['email'] = $request->email;
        $user->save();

        //同时写入用户详情
        $userdetail = new Userdetail;
        $userdetail['nickName']=$request->name;
        $userdetail ->save();

        return redirect('/admin/user');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
   public function edit(Request $request,$id)
    {
        //接收ajax请求带来的status  修改数据库中status
        $status = $request->status;
        $user = User::findOrFail($id);

        if($status == '0'){
            $user->update(['status'=>1]);
            return '1';
        }else{
            $user->update(['status'=>0]);
            return '0';
        }

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $user = User::findOrFail($id);
        $user->delete();
        $userdetail = Userdetail::findOrFail($id);
        $userdetail->delete();
        return back();
    }
}
